package com.sxc.OnlineStore.goods.servlet;

import com.sxc.OnlineStore.goods.dao.GoodsPayIm;
import com.sxc.OnlineStore.goods.domain.GoodsPay;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
<<<<<<< HEAD
import java.io.IOException;
=======
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
>>>>>>> 还没有实现在servlet中获取sessiond
import java.util.ArrayList;

@WebServlet("/BuyGoodsServlet")
public class BuyGoodsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //定义并得到前端传过来的值
        String op = request.getParameter("op");
<<<<<<< HEAD
        String goods_details = request.getParameter("goods_details");
        String goods_price = request.getParameter("goods_price");
        String goods_number = request.getParameter("goods_number");
        //String  = request.getParameter("");

        GoodsPay goodsPay = new GoodsPay();

        if (op.equals("addGoods")){
            GoodsPayIm goodsPayIm = new GoodsPayIm();
            goodsPayIm.GoodsPayInstant(goods_details,goods_price,goods_number);
            //传送值到下一个支付下单界面
            request.setAttribute("goodsDetails",goods_details);
            request.setAttribute("goodsPrice",goods_price);
            request.setAttribute("goodsNumber",goods_number);
            request.getRequestDispatcher("goods_pay.jsp").forward(request,response);
        }

        if (op.equals("showGoods")){

            GoodsPayIm goodsPayIm = new GoodsPayIm();
            ArrayList<GoodsPay> goodsPayArrayList = goodsPayIm.QueryGoodsPay();
            request.setAttribute("goodsPayArrayList",goodsPayArrayList);
            request.getRequestDispatcher("goods_pay.jsp").forward(request,response);

        }





=======
        String goodsPK = request.getParameter("goodsPK");//获取goods的主键

        GoodsPay goodsPay = new GoodsPay();
        PrintWriter out = response.getWriter();//定义输出到jsp的对象
        //创建session会话
        HttpSession session = request.getSession();
        String userPK = (String) session.getAttribute("userPK");//获取session中user的主键
        System.out.println(userPK);
        //检测用户登录状态session会话是否创建了，得到的userID为空，就跳转到登录界面
        if (userPK == null){
            out.write("<script>alert(\"还没有登录哦~~请先登录账号哈~\");location.replace(\"user_login.jsp\");</script>");

        }else {
            //user的session已经存在了所以可以继续访问
            if (op.equals("addGoodsPayAndShow")){
                GoodsPayIm goodsPayIm = new GoodsPayIm();
                goodsPayIm.GoodsPayInstant(userPK,goodsPK);
                //传送值到下一个支付下单界面
                //request.setAttribute("goodsPK",goodsPK);
                ArrayList<GoodsPay> goodsPayArrayList = goodsPayIm.QueryGoodsPay(userPK,goodsPK);
                request.setAttribute("goodsPayArrayList",goodsPayArrayList);

                request.getRequestDispatcher("goods_pay.jsp").forward(request,response);
            }

        }
>>>>>>> 还没有实现在servlet中获取sessiond
    }
}
